package cn.jiangjiesheng.bootstrap.fileport.interfaces.model;

import com.deepoove.poi.data.*;
import com.deepoove.poi.data.style.TableStyle;
import com.fasterxml.jackson.databind.JsonNode;
import cn.jiangjiesheng.bootstrap.fileport.domain.base.EChart;
import io.swagger.annotations.ApiModel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by dev@jiangjiesheng.cn on 2021/3/16  <br/>
 *
 * @author jerrylau
 */
@ApiModel(value = "数据数据")
public class DocDataModel extends HashMap<String, Object> {

    public String getKey(String key) {
        return key;
    }

    /**
     * https://echarts.apache.org/en/option.html chart参数组织方式
     *
     * @param key
     * @param options
     */
    public DocDataModel setEChart(String key, JsonNode options, int width, int height) {
        this.setImage(getKey(key), Pictures.ofBase64(new EChart(options, width, height).toBase64(), PictureType.PNG)
                .size(width, height)
                .create());
        return this;
    }

    /**
     * 添加表格
     *
     * @param key
     * @param tableData
     */
    public DocDataModel setTable(String key, String[][] tableData) {
        List<RowRenderData> rows = new LinkedList<>();
        String[] columns = tableData[0];
        rows.add(Rows.of(columns).center().textBold().rowHeight(3).create()); // header
        for (int i = 1; i < tableData.length; i++) {
            rows.add(i, Rows.of(tableData[i]).center().rowHeight(1.4).create());
        }
        TableRenderData tableRenderData = Tables.of(rows.toArray(new RowRenderData[tableData.length]))
                .border(TableStyle.BorderStyle.DEFAULT)
                .create();
        this.setTable(key, tableRenderData);
        return this;
    }

    /**
     * 添加表格
     *
     * @param key
     * @param tableData
     */
    public DocDataModel setTable(String key, TableRenderData tableData) {
        this.put(getKey(key), tableData);
        return this;
    }

    /**
     * 添加文本
     *
     * @param key
     * @param text
     */
    public DocDataModel setText(String key, String text) {
        this.put(getKey(key), text);
        return this;
    }

    public DocDataModel setText(String key, TextRenderData textRenderData) {
        this.put(getKey(key), textRenderData);
        return this;
    }

    public DocDataModel setImage(String key, PictureRenderData pictureRenderData) {
        this.put(getKey(key), pictureRenderData);
        return this;
    }

    /**
     * 添加一张图片
     *
     * @param key
     */
    public DocDataModel setImage(String key, File file, int width, int height) {
        this.put(getKey(key), new PictureRenderData(width, height, file));
        return this;
    }

    public DocDataModel setImage(String key, String localFilePath, int width, int height) {
        this.put(getKey(key), new PictureRenderData(width, height, localFilePath));
        return this;
    }

    public DocDataModel setImageFromUrl(String key, String url, int width, int height) {
        PictureType pictureType = null;
        if(url.contains("png")){
            pictureType = PictureType.PNG;
        }else if (url.contains("jpg")){
            pictureType = PictureType.JPEG;
        }else if(url.contains("gif")){
            pictureType = PictureType.GIF;
        } else {
            pictureType = PictureType.JPEG;
        }
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(new URL(url));
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.put(getKey(key), new PictureRenderData(width, height, pictureType, bufferedImage));
        return this;
    }
}
